using System;
using System.Data;
using System.Configuration;

using System.Collections;
using System.Web;
using System.Web.Security;

using System.Web.UI;
using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;
using Component;
public partial class BackEnd_CategorySubList : System.Web.UI.UserControl
{
    public string _Session = "";
    int _IdCat = 0;
    public string _Url = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        _Url = Request.Url.ToString().Split('&')[0] + "&" + Request.Url.ToString().Split('&')[1] + "&" + ConstString._Del + "=" + ConstString._Del + "&" + ConstString._idsubcat + "=";
        if (QueryHelper.GetQueryString(Request, ConstString._Del) == ConstString._Del)
        {
            int idCatSub  = ConvertType.ToInt(QueryHelper.GetQueryString(Request, ConstString._idsubcat));
            CategorySub catSub = CategorySubDB.GetObjectByid(idCatSub, true);
            if (CategorySubDB.Delete(catSub) == 1)
                this.labMessage.Text = ConstString.xoaTC;
        }
        
        _IdCat = ConvertType.ToInt(QueryHelper.GetQueryString(Request, ConstString._idcat));
        Category cat1 = CategoryDB.GetObjectByid(_IdCat, true);
        if (cat1 != null)
            Sessions.CatSub = _Session = Sessions.Cat + " --> <a href=\"" + Request.RawUrl + "\" >" + cat1.Name + "</a>";
        DataBind(CategorySubDB.GetCategorySubByIdCat(_IdCat, true));
        grid.RowCancelingEdit += delegate { Response.Redirect(Request.Url.ToString()); };
    }
    #region ----------------------------- Ham viet tay -------------------------------------
    void DataBind(object al)
    {
        if (al != null)
        {
            grid.DataKeyNames = new string[] { ColumCategorySub._Id };
            grid.DataSource = al;
            grid.DataBind();
        }
    }
    #endregion

    protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        object id = grid.DataKeys[e.RowIndex].Value;
        TextBox txtName = (TextBox)grid.Rows[e.RowIndex].FindControl("txtName");
        TextBox txtNameEL = (TextBox)grid.Rows[e.RowIndex].FindControl("txtNameEL");
        DropDownList ddlAct = (DropDownList)grid.Rows[e.RowIndex].FindControl("ddlAct");
        TextBox txtPriority = (TextBox)grid.Rows[e.RowIndex].FindControl("txtPriority");
        CategorySub catSub = new CategorySub(KetNoiCSDL.session);
        catSub = CategorySubDB.GetObjectByid(ConvertType.ToInt(id), true);
        catSub.Name = txtName.Text.Trim();
        catSub.NameEL = txtNameEL.Text.Trim();
        catSub.Priority = ConvertType.ToInt(txtPriority.Text.Trim());
        catSub.Active = ConvertType.ToBool(ddlAct.Text);
        if (CategorySubDB.Save(catSub) == 1)
        {
            this.labMessage.Text = ConstString.capNhatTC;

        }
        Response.Redirect(Request.Url.ToString());
    }
    protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            object id = grid.DataKeys[e.RowIndex].Value;
            CategorySub cat = CategorySubDB.GetObjectByid(ConvertType.ToInt(id), true);
            if (CategorySubDB.Delete(cat) == 1)
                this.labMessage.Text = ConstString.xoaTC;
        }
        catch { }
        DataBind(CategorySubDB.GetCategorySubByIdCat(_IdCat, true));
    }
    protected void grid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grid.EditIndex = e.NewEditIndex;
        DataBind(CategorySubDB.GetCategorySubByIdCat(_IdCat, true));
    }
    protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grid.PageIndex = e.NewPageIndex;
        DataBind(CategorySubDB.GetCategorySubByIdCat(_IdCat, true));
    }
}
